package com.jfinal.model.generator;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.generator.BaseModelGenerator;
import com.jfinal.plugin.activerecord.generator.ColumnMeta;
import com.jfinal.plugin.activerecord.generator.TableMeta;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * Created by liuandong on 2017/11/17.
 */
public class BaseModelAndInterfaceGen extends BaseModelGenerator {

    protected String classDefineTemplate = "/**%n * Generated by SqlBuilder, do not modify this file.%n */%n@SuppressWarnings(\"serial\")%npublic abstract class %s<M extends %s<M>> extends Model<M> implements IBean,I%s {%n%n";
    public String classDefineForSetterChainTemplate = "/**%n * Generated by SqlBuilder, do not modify this file.%n */%n@SuppressWarnings({\"serial\", \"unchecked\"})%npublic abstract class %s<M extends %s<M>> extends Model<M> implements IBean,I%s {%n%n";

    public String interfaceDefine = "public interface I%s{\n";

    public BaseModelAndInterfaceGen(String baseModelPackageName, String baseModelOutputDir) {
        super(baseModelPackageName, baseModelOutputDir);
    }

    protected void genClassDefine(TableMeta tableMeta, StringBuilder ret) {
        String template = this.generateChainSetter?this.classDefineForSetterChainTemplate:this.classDefineTemplate;
        ret.append(String.format(template, new Object[]{tableMeta.baseModelName, tableMeta.baseModelName, tableMeta.modelName}));
    }

    protected void writeToFile(List<TableMeta> tableMetas) {
        try {
            Iterator var2 = tableMetas.iterator();

            while(var2.hasNext()) {
                TableMeta tableMeta = (TableMeta)var2.next();
                this.writeToFile(tableMeta);
            }

        } catch (IOException var4) {
            throw new RuntimeException(var4);
        }
    }

    protected void writeToFile(TableMeta tableMeta) throws IOException {
        File dir = new File(this.baseModelOutputDir);
        if(!dir.exists()) {
            dir.mkdirs();
        }

        String target = this.baseModelOutputDir + File.separator + tableMeta.baseModelName + ".java";
        FileWriter fw = new FileWriter(target);

        try {
            fw.write(tableMeta.baseModelContent);
        } finally {
            fw.close();
        }

        //写入接口
        String target2 = this.baseModelOutputDir + File.separator + "I" + tableMeta.modelName + ".java";
        FileWriter fw2 = new FileWriter(target2);

        try {
            fw2.write(getInterfaceFileString(tableMeta));
        } finally {
            fw2.close();
        }
    }


    private String getInterfaceFileString(TableMeta tableMeta){
        StringBuilder sb = new StringBuilder();
        genPackage(sb);
        genInterfaceDefine(tableMeta,sb);
        tableMeta.columnMetas.forEach(column -> genStaticAttrName(column,sb));
        sb.append("}");
        return sb.toString();
    }

    protected void genInterfaceDefine(TableMeta tableMeta, StringBuilder ret) {
        ret.append(String.format(interfaceDefine, new Object[]{tableMeta.modelName}));
    }

    protected void genStaticAttrName(ColumnMeta columnMeta, StringBuilder ret) {
        ret.append("    public static final String _" + columnMeta.attrName + " = \""+ columnMeta.name +"\";\n");
    }


}
